package com.xbdun.comweb.bmp.freemark;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.alibaba.fastjson.JSON;
import com.xbdun.comweb.bmp.util.WebCommonUtil;

import freemarker.core.Environment;
import freemarker.template.Template;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateScalarModel;

/**
*   <!-- 上传文件控件
<UploadFile callback="[{\"targetId\":\"targetId\",\"type\":1001,\"callback\":\"function(data){alert(data)}\"},{\"targetId\":\"targetId2\",\"type\":1001,\"callback\":\"function(data){alert(data)}\"}]"UploadFile> -->
<@UploadFile isCutImg="true" callback="[{\"targetId\":\"targetId\",\"type\":1001,\"callback\":\"function(data){alert(data)}\"}]"></@UploadFile>
* @ClassName: UploadFileDirective
* @Description: 上传文件
* @author zhanggt 360730440@qq.com
* @date 2017年5月15日 下午1:11:05
*/
public class UploadFileDirective implements TemplateDirectiveModel {
	@Autowired  
    private FreeMarkerConfigurer freeMarkerConfigurer;
	public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,TemplateDirectiveBody body) throws TemplateException, IOException {
		  String jsonHtml = null;//JSONHTML
		  Template template = null;//模板freemark
		  List<UploadFmkVo> fmkFunction = null;//上传回调函数
		  //设置上传文件回调阐述
		  Map<String,Object> fkParams = new HashMap<String,Object>();
		  Object waterButton = params.get("waterButton");//是否存在水印
		  Object cutImg = params.get("isCutImg");//是否为切图控件
		  Object fileSizeLimit = params.get("fileSizeLimit");//上传文件大小，默认1M
		  Object multi = params.get("isMulti");//是否为多文件上传
		  
		  if(params.get("callback") instanceof TemplateScalarModel){
			  jsonHtml = ((TemplateScalarModel)params.get("callback")).getAsString();
		  }
		  //获取上传文件
		  if(null!=jsonHtml){
			  fmkFunction = JSON.parseArray(jsonHtml, UploadFmkVo.class);
		  }
		  //不是切图上传
		  if(null!=cutImg&&("true".equals(String.valueOf(cutImg).toLowerCase()))){
			  template = freeMarkerConfigurer.getConfiguration().getTemplate("/_sys/upload/cutimage.ftl","UTf-8");
		  }else{
			  template = freeMarkerConfigurer.getConfiguration().getTemplate("/_sys/upload/fileupload.ftl","UTf-8"); 
		  }
		  //上传文件大小
		  if(null!=fileSizeLimit){
			  fkParams.put("fileSizeLimit", Integer.valueOf(fileSizeLimit+""));
		  }
		  //是否存在水印
		  if(null!=waterButton&&("true".equals(String.valueOf(waterButton).toLowerCase()))){
			  fkParams.put("waterButton",true);
		  }
		  //是否多文件上传
		  if(null!=multi&&("true".equals(String.valueOf(multi).toLowerCase()))){
			  fkParams.put("isMulti",true);
		  }
		  fkParams.put("fnCallBack",fmkFunction);
		  fkParams.put("cxt", WebCommonUtil.getBaseUrl());			 //本地地址
		  fkParams.put("valCxt",WebCommonUtil.getWEB_VALIDATE_URL());//第三方平台地址
		  //读取到真实HTML
		  String html = FreeMarkerTemplateUtils.processTemplateIntoString(template,fkParams);
		  env.getOut().write(html);
	}
}
